package com.huluobo.a2210mdmdemo.demo

import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.grid.GridCells
import androidx.compose.foundation.lazy.grid.LazyHorizontalGrid
import androidx.compose.foundation.lazy.grid.LazyVerticalGrid
import androidx.compose.foundation.lazy.grid.items
import androidx.compose.foundation.lazy.items
import androidx.compose.foundation.lazy.staggeredgrid.LazyVerticalStaggeredGrid
import androidx.compose.foundation.lazy.staggeredgrid.StaggeredGridCells
import androidx.compose.foundation.lazy.staggeredgrid.items
import androidx.compose.foundation.shape.CircleShape
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.unit.dp
import androidx.lifecycle.LifecycleCoroutineScope
import coil.compose.AsyncImage
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.GlobalScope

/**
 *  Created by LC on 2025/3/24.
 */

data class Student(val name: String, val desc: String, val img: String)

@Composable
fun Demo03() {
    val list = mutableListOf<Student>()

    for (i in 1..20) {
        list.add(
            Student(
                "张三$i",
                "这是我的个人简介:$i",
                "https://img1.baidu.com/it/u=4214113141,2036563917&fm=253&app=120&size=w931&n=0&f=JPEG&fmt=auto?sec=1729098000&t=0662d2b383091882ccdd43ad060de2a7"
            )
        )
    }


    //LazyColumn 纵向列表,纵向列表只有纵向间隔属性,横向只能选择左对齐右对齐或者居中
    //LazyRow 横向列表,只有横向间隔属性,纵向可以顶部对齐,底部对齐,和横向间隔
    //items 注意选择第二个参数为list的导包
//    LazyColumn(Modifier.padding(horizontal = 20.dp), verticalArrangement = Arrangement.spacedBy(20.dp),
//        content = {
//            //相当于adapter
//            items(list) {
//                StudentItem(student = it)
//            }
//        })
//    LazyVerticalGrid(
//        columns = GridCells.Fixed(2),
//        verticalArrangement = Arrangement.spacedBy(20.dp),
//        horizontalArrangement = Arrangement.spacedBy(20.dp),
//        content = {
//            items(list) {
//                StudentItem(student = it)
//            }
//        }, modifier = Modifier.padding(20.dp)
//    )

//    LazyHorizontalGrid(
//        rows = GridCells.Fixed(3),
//        verticalArrangement = Arrangement.spacedBy(20.dp),
//        horizontalArrangement = Arrangement.spacedBy(20.dp),
//        content = {
//            items(list) {
//                StudentItem(student = it)
//            }
//        }, modifier = Modifier.padding(20.dp)
//    )

    LazyVerticalStaggeredGrid(columns = StaggeredGridCells.Fixed(3), content = {
        items(list) {
            StudentItem(student = it)
        }
    })
}

//相当于RecyclerView的item
@Composable
fun StudentItem(student: Student) {
    Row {
        AsyncImage(
            model = student.img,
            contentDescription = "",
            contentScale = ContentScale.Crop,
            modifier = Modifier
                .size(80.dp)
                .clip(
                    CircleShape
                )
        )
        //中间给一个10dp的空格
        Spacer(modifier = Modifier.width(10.dp))
        Column(Modifier.height(80.dp), verticalArrangement = Arrangement.SpaceAround) {
            Text(text = student.name)
            Text(text = student.desc)
        }
    }
}